﻿var CalculatorMenu = function (nationalItem, cityItem, restaurantItem, restaurantTypeItem,
            menuElement, menuTypeElement, deginDate, priceAdultsMenu, priceChildsMenu,
    quantityAdultsMenu, quantityChildMenu, vatMenu, totalPriceMenu, note, seasonTour, tourInfo) {
    $(priceAdultsMenu).val(0);
    $(priceChildsMenu).val(0);
    $(quantityAdultsMenu).val(0);
    $(quantityChildMenu).val(0);
    $(vatMenu).val(0);
    //load thực đơn
    var loadByMenu = function () { 
        var typeId = 4;
        var restaurantId = $(restaurantItem).val();
        var menuTypeElementid = $(menuTypeElement).val();
        var date = $(deginDate).val();
        var season = seasonTour;
        if (restaurantId <= 0 || menuTypeElementid <= 0 || date == NaN) {
            $(menuElement).html('<option value="-1">--- Chọn thực đơn ---</option>'); return;
        }
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "PriceListByMenu", "TypeId": typeId, "RestaurantId": restaurantId, "MenuTypeId": menuTypeElementid, "SeasonTour": season, "Date": date },
            dataType: "json",
            success: function (data) {
                $(menuElement).html('<option value="-1">--- Chọn thực đơn ---</option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(menuElement)
                            .append($("<option></option>")
                                .attr("value", item.PriceId)
                                .text(item.PriceMenu));
                    });
                }
            }
        });
    };
    // goi để load thực đơn
    $(restaurantItem).change(loadByMenu);
    $(deginDate).change(loadByMenu);
    $(menuTypeElement).change(loadByMenu);
    //load thực đơn
    var loadPricel = function () {
        var menuElementId = $(menuElement).val();
       
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "PriceTicKetAri", "TicketId": menuElementId },
            dataType: "json",
            success: function (data) {
                if (data && data.isError == false) {
                    $(priceAdultsMenu).val(data.PriceAdult);
                    $(priceChildsMenu).val(data.PriceChild);
                    funTong();
                }
            }

        });
    };
    var funTong = function () {
        var priceAdults = $(priceAdultsMenu).val();
        var priceChilds = $(priceChildsMenu).val();
        var quantityAdults = $(quantityAdultsMenu).val();
        var quantityChild = $(quantityChildMenu).val();
        var vat = $(vatMenu).val();
        var total = 0;
        if (vat > 0 || vat < 100) {
            total = ((parseInt(priceAdults) * parseInt(quantityAdults)) + (parseInt(priceChilds) * parseInt(quantityChild)));
            total = total + (total * vat / 100);
        }
        $(totalPriceMenu).val(total);
    };

    // goi load gia
    $(menuElement).change(loadPricel);
    $(quantityAdultsMenu).keyup(loadPricel);
    $(quantityChildMenu).keyup(loadPricel);
    $(priceAdultsMenu).keyup(loadPricel);
    $(priceChildsMenu).keyup(loadPricel);
    $(vatMenu).keyup(loadPricel);

    $('#btnInsertMenu').live("click", function () {

        var typeId = 4;
        var tourId = parseInt(tourInfo);
        var dateMenu = $(deginDate).val();
        var menuElementId = $(menuElement).val();
        var priceAdults = $(priceAdultsMenu).val();
        var priceChilds = $(priceChildsMenu).val();
        var quantityAdults = $(quantityAdultsMenu).val();
        var quantityChild = $(quantityChildMenu).val();
        var vat = $(vatMenu).val();
        if (vat > 0 && vat <= 100) { } else { vat = 0; }
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: {
                "function": "InsertMenu", "TypeId": typeId, "TourId": tourId, "menuElementID": menuElementId,
                "PriceAdults": priceAdults, "PriceChilds": priceChilds, "Date": dateMenu,
                "CalculatorId": calculatorId, "ClassName": className, 'Vat': vat,
                'QuantityAdults': quantityAdults, 'QuantityChild': quantityChild
            },
            dataType: "json",
            success: function (data) {

                $(".ListMenu").remove();
             
                $(nationalItem).val('-1');
                $(cityItem).val('-1');
                $(restaurantItem).val('-1');
                $(restaurantTypeItem).val('-1');
                $(menuElement).val('-1');
                $(menuTypeElement).val('-1');
                $(deginDate).val('');
                $(priceAdultsMenu).val('0');
                $(priceChildsMenu).val('0');
                $(quantityAdultsMenu).val('0');
                $(quantityChildMenu).val('0');
                $(vatMenu).val('0');
                $(totalPriceMenu).val('0');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        var totaIitem = (parseInt(item.PriceAdults) * parseInt(item.QuantityAdults)) + (parseInt(item.PriceChild) * parseInt(item.QuantityChild));
                        totaIitem = totaIitem + (totaIitem * item.Vat / 100);
                        $('#new-Menu')
                            .before($("<tr class='ListMenu' id='menu-" + item.CalculatorId + "'></tr>").html('<td></td>' +
                                '<td >' + item.RestaurantTypeName + '</td>' +
                                '<td >' + item.NationalName + '</td>' +
                                '<td >' + item.CityName + '</td>' +
                                '<td >' + item.RestaurantFullName + '</td>' +
                                '<td >' + item.DateBegin + '</td>' +
                                '<td >' + item.MenuTypeName + '</td>' +
                                '<td >' + item.MenuName + '</td>' +
                                '<td >' + item.PriceAdults + '</td>' +
                                '<td >' + item.PriceChild + '</td>' +
                                '<td >' + item.QuantityAdults + '</td>' +
                                '<td >' + item.QuantityChild + '</td>' +
                                '<td >' + item.Vat + '</td>' +
                                '<td >' + totaIitem + '</td>' +
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateMenu"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteMenu"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));
                    });
                    $('#TotalMenuPrices').html(parseInt(data.TotalMenuChild) + parseInt(data.TotalMenuAdults));
                    $('#TotalMenuAdults').html(data.TotalMenuAdults);
                    $('#TotalMenuChild').html(data.TotalMenuChild); 
                
                    $('#btnInsertMenu').attr("calculatorid", "-1").val("Thêm");
                    $('.btnDeleteMenu').show();
                    $('#btnDeleteMenu').hide();
                }

            }
        });

    });
    $('.btnUpdateMenu').live("click", function () {
        var calculatorId = $(this).attr("calculatorid"); 
        var callAjax = $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "UpdateMenu", "CalculatorId": calculatorId },
            dataType: "json",
            success: function (data) {
           
                $('#menu-' + calculatorId).remove();
                $(nationalItem).val(data.Nationalid);
                loadCity_Nationalid(nationalItem, cityItem, data.CityId, restaurantTypeItem, restaurantItem, data.RestaurantId, menuTypeElement, deginDate, seasonTour, menuElement, data.MenuId);
            
                $(restaurantTypeItem).val(data.RestaurantTypeId);
                //$(menuElement).val(data.MenuId);
                $(menuTypeElement).val(data.MenuTypeId);
                $(deginDate).val(data.DateBegin);
                $(priceAdultsMenu).val(data.PriceAdults);
                $(priceChildsMenu).val(data.PriceChild);
                $(quantityAdultsMenu).val(data.QuantityAdults);
                $(quantityChildMenu).val(data.QuantityChild);
                $(vatMenu).val(data.Vat);
                $(totalPriceMenu).val(data.TotalPriceMenu); 
                
                $('#btnInsertMenu').attr("calculatorid", calculatorId).val("Lưu");
                $('.btnUpdateMenu').hide();
                $('#btnDeleteMenu').show();
            }
        });
    });
    $('.btnDeleteMenu').live("click", function () { 
        var typeId = 4;
        var tourId = parseInt(tourInfo);
        var dateMenu = $(deginDate).val();
        var menuElementId = $(menuElement).val();
        var priceAdults = $(priceAdultsMenu).val();
        var priceChilds = $(priceChildsMenu).val();
        var quantityAdults = $(quantityAdultsMenu).val();
        var quantityChild = $(quantityChildMenu).val();
        var vat = $(vatMenu).val();
        if (vat > 0 && vat <= 100) { } else { vat = 0; } 
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: {
                "function": "InsertMenu", "TypeId": typeId, "TourId": tourId, "menuElementID": menuElementId,
                "PriceAdults": priceAdults, "PriceChilds": priceChilds, "Date": dateMenu,
                "CalculatorId": calculatorId, "ClassName": className, 'Vat': vat,
                'QuantityAdults': quantityAdults, 'QuantityChild': quantityChild
            },
            dataType: "json",
            success: function (data) { 
                $(".ListMenu").remove(); 
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        var totaIitem =  (parseInt(item.PriceAdults) * parseInt(item.QuantityAdults)) + (parseInt(item.PriceChild) * parseInt(item.QuantityChild)) ;
                        totaIitem = totaIitem + (totaIitem * item.Vat / 100);
                        $('#new-Menu')
                             .before($("<tr class='ListMenu' id='menu-" + item.CalculatorId + "'></tr>").html('<td></td>' +
                                 '<td >' + item.RestaurantTypeName + '</td>' +
                                 '<td >' + item.NationalName + '</td>' +
                                 '<td >' + item.CityName + '</td>' +
                                 '<td >' + item.RestaurantFullName + '</td>' +
                                 '<td >' + item.DateBegin + '</td>' +
                                 '<td >' + item.MenuTypeName + '</td>' +
                                 '<td >' + item.MenuName + '</td>' +
                                 '<td >' + item.PriceAdults + '</td>' +
                                 '<td >' + item.PriceChild + '</td>' +
                                 '<td >' + item.QuantityAdults + '</td>' +
                                 '<td >' + item.QuantityChild + '</td>' +
                                 '<td >' + item.Vat + '</td>' +
                                '<td >' + totaIitem + '</td>' +
                                 '<td></td>' +
                                 '<td>' +
                                 '<input class="btnUpdateMenu"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                 '<input class="btnDeleteMenu"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                 '</td>'));
                    });
                    $('#TotalMenuPrices').html( parseInt(data.TotalMenuChild ) +parseInt( data.TotalMenuAdults));
                    $('#TotalMenuAdults').html(data.TotalMenuAdults);
                    $('#TotalMenuChild').html(data.TotalMenuChild); 
                    
                    $('#btnInsertMenu').attr("calculatorid", "-1").val("Thêm");
                    $('.btnDeleteMenu').show();
                    $('#btnDeleteMenu').hide();
                } 
            }
        }); 
    });
};

